<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加管理员</title>
    <link rel="stylesheet" type="text/css" href="__STATIC__/plugins/layui/css/layui.css">
    <script type="text/javascript" src="__STATIC__/plugins/layui/layui.js"></script>
</head>
<body style="padding: 10px;">
    <form class="layui-form">
        <input type="hidden" name="type" value="0">
        <input type="hidden" name="id" value="{$data.id}">
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="title" value="{$data.title}"}>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="ord" value="{$data.ord}"}>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">URL</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" name="url" value="{$data.url}"}>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">图片上传</label>
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-sm"onclick="return false;" id="upload_img">上传</button>
                <img id="preview_img" {$data['img']?'src="'.$data['img'].'"':''} style="height: 30px;" />
                <input type="hidden" name="img"  value="{$data.img}" />
            </div>
        </div>

    </form>
    <hr/>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" onclick="save()">保存</button>
        </div>
    </div>

    <script type="text/javascript">
        layui.use(['layer','form','upload'],function () {
            form = layui.form;
            layer = layui.layer;
            upload = layui.upload;
            $ = layui.jquery;

            //执行实例
            var uploadInst = upload.render({
                elem: '#upload_img' //绑定元素
                ,url: "{:url('admins/Video/upload_img')}" //上传接口
                ,done: function(res){
                    //上传完毕回调
                    $('#preview_img').attr('src',res.msg);
                    $('input[name="img"]').val(res.msg);

                }
                ,error: function(){
                    //请求异常回调
                }
            });
        });

        // 保存幻灯片
        function save() {
            var id = parseInt($('input[name="id"]').val());
            var title = $.trim($('input[name="title"]').val());
            var url = $.trim($('input[name="url"]').val());
            var img = $.trim($('input[name="img"]').val());


            if(title == ''){
                layer.alert('请输入标题',{icon:2});
                return;
            }
            if(url == ''){
                layer.alert('请输入url',{icon:2});
                return;
            }
            if(img==''){
                layer.alert('请上传图片',{icon:2});
                return;
            }

            // 发送请求
            $.post("{:url('admins/Slide/save')}",$('form').serialize(),function (res) {
                // var res = JSON.parse(res);  //字符串转json对象
                if(res.code>0){
                    layer.alert(res.msg,{icon:2});
                }else{
                    layer.msg(res.msg,{icon:1});
                    setTimeout(function(){parent.window.location.reload();},500);
                }
            },'json');
        }
    </script>
</body>
</html>